package com.ruoyi.leaveStudent.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.leaveManage.bo.LeaveManageQueryBo;
import com.ruoyi.leaveManage.mapper.LeaveManageMapper;
import com.ruoyi.leaveManage.service.ILeaveManageService;
import com.ruoyi.leaveManage.vo.LeaveUserVo;
import com.ruoyi.leaveManage.vo.LeaveVo;
import com.ruoyi.leaveStudent.bo.LeaveAddBo;
import com.ruoyi.leaveStudent.service.ILeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Api(value = "学生请假控制器", tags = {"学生请假信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/leavestudent/student")
public class StudentController extends BaseController {
    private final ILeaveService iLeaveService;

    private final ILeaveManageService iLeaveManageService;

    private final LeaveManageMapper leaveManageMapper;

    /**
     * 查询请假信息列表
     */
    @ApiOperation("查询请假信息列表")
    @PreAuthorize("@ss.hasPermi('leava:student:list')")
    @GetMapping("/list")
    public TableDataInfo<LeaveUserVo> list(LeaveManageQueryBo bo) {
        startPage();
        List<LeaveVo> list = iLeaveManageService.queryList(bo);
        List list1=new ArrayList();
        for (LeaveVo leaveVo:list){
            list1.add(BeanUtil.toBean(leaveVo, LeaveUserVo.class));
        }
        return getDataTable(list1);
    }

    /**
     * 查询请假报表
     */
    @ApiOperation("查询请假报表")
    @PreAuthorize("@ss.hasPermi('leava:student:reportFromList')")
    @Log(title = "查询请假报表", businessType = BusinessType.INSERT)
    @GetMapping("/reportFromList")
    public TableDataInfo<LeaveUserVo> reportFromList(LeaveManageQueryBo bo) {

        startPage();
        bo.setTimeSlot(1);
        List<LeaveUserVo> list = iLeaveManageService.getReportFromList(bo);

        return getDataTable(list);
    }

    /**
     * 新增请假信息
     */
    @ApiOperation("新增请假信息")
    @PreAuthorize("@ss.hasPermi('leava:student:add')")
    @Log(title = "请假信息", businessType = BusinessType.INSERT)
    @PutMapping()
    public AjaxResult<Void> add(@RequestBody LeaveAddBo bo) {
        return toAjax(iLeaveService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 获取请假信息详细信息
     */
    @ApiOperation("获取请假信息详细信息")
    @PreAuthorize("@ss.hasPermi('leava:student:query')")
    @GetMapping("/{leaveId}")
    public AjaxResult<LeaveVo> getInfo(@PathVariable("leaveId" ) Long leaveId) {
        return AjaxResult.success(iLeaveManageService.queryById(leaveId));
    }

    /**
     * 撤销假条
     */
    @ApiOperation("撤销假条")
    @PreAuthorize("@ss.hasPermi('leava:student:query')")
    @GetMapping("/{leaveIdRevoke}")
    public AjaxResult<Void> leaveRevoke(@PathVariable("leaveId" ) Integer leaveId) {
        return toAjax(iLeaveService.updateByLeaveRevoke(leaveId)? 1 : 0);
    }


    /**
     * 注销假条
     */
    @ApiOperation("撤销假条")
    @PreAuthorize("@ss.hasPermi('leava:student:query')")
    @GetMapping("/{cancellationStatus}")
    public AjaxResult<Void> leaveCancellationStatus(@PathVariable("cancellationStatus" ) Integer cancellationStatus) {
        return toAjax(iLeaveService.updateByCancellationStatus(cancellationStatus)? 1 : 0);
    }
}
